
// import * as d3 from "../lib/d3.js";
import * as d3 from "d3";
import {$} from "../util/seq.js";
import {debounce} from "underscore";
import {isEqual} from "underscore";

// export const emSize = parseFloat(getComputedStyle(document.querySelector("html"))["font-size"]);

/** @returns the bounds of the display */
export function computeViewbox() {
    // const {clientWidth: width, clientHeight: height} = document.getElementById("display");
    // innerWidth/innerHeight includes the size of scrollbars that appear when reducing window size
    const {innerWidth: width, innerHeight: height} = window;
    return {xMin: 0, yMin: 0, xMax: width - 1, yMax: height - 1, width, height};
}

/**
 * @param viewboxAgent
 * @returns the newly computed viewbox
 */
export function defineViewbox(viewboxAgent) {

    function updateViewbox() {
        const viewbox = computeViewbox();
        if (!isEqual(viewbox, viewboxAgent.value())) {
            viewboxAgent.submit(viewbox);
        }
    }

    d3.select(window).on($`resize`, debounce(updateViewbox, 2000));

    // we assume the agent has already been initialized with the initial viewbox
    // updateViewbox();
}
